<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input v-model="query.name" clearable size="small" placeholder="输入名称搜索" style="width: 200px;"
                  class="filter-item" @keyup.enter.native="crud.toQuery"/>
        <rrOperation/>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">

        <template slot="right">
          <el-button
            class="filter-item"
            size="mini"
            type="success"
            @click="pictureManagement(crud.selections[0])"
            :disabled="crud.selections.length !== 1"
          >
            图片管理
          </el-button>
        </template>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="药品分类">
            <el-select v-model="form.type" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.single_classification"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="名称(蒙)" prop="mnName">
            <el-input v-model="form.mnName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="名称(汉)" prop="zhName">
            <el-input v-model="form.zhName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="名称(英)">
            <el-input v-model="form.elName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="别名(蒙)">
            <el-input v-model="form.mnAnotherName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="别名(汉)">
            <el-input v-model="form.zhAnotherName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="别名(英)">
            <el-input v-model="form.elAnotherName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="学名(蒙)">
            <el-input v-model="form.mnScientificName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="学名(汉)">
            <el-input v-model="form.zhScientificName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="学名(英)">
            <el-input v-model="form.elScientificName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="文献来源(蒙)">
            <el-input v-model="form.mnDocument" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item label="文献来源(汉)">
            <el-input v-model="form.zhDocument" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item label="生境(蒙)">
            <el-input v-model="form.mnHabitat" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item label="生境(汉)">
            <el-input v-model="form.zhHabitat" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item label="入药部分(蒙)">
            <el-input v-model="form.mnMedicatedPart" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="入药部分(汉)">
            <el-input v-model="form.zhMedicatedPart" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="性味(蒙)">
            <el-input v-model="form.mnNatureTaste" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="味性(汉)">
            <el-input v-model="form.zhNatureTaste" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="功能(蒙)">
            <el-input v-model="form.mnFeature" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item label="功能(汉)">
            <el-input v-model="form.zhFeature" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item label="主治(蒙)">
            <el-input v-model="form.mnFunctionsDisease" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="主治(蒙)">
            <el-input v-model="form.zhFunctionsDisease" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="副作用(蒙)">
            <el-input v-model="form.mnSideEffect" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item label="副作用(汉)">
            <el-input v-model="form.zhSideEffect" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item label="临床使用(蒙)">
            <el-input v-model="form.mnClinicalUse" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="临床使用(汉)">
            <el-input v-model="form.zhClinicalUse" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="解释(蒙)">
            <el-input v-model="form.mnExplain" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item label="解释(汉)">
            <el-input v-model="form.zhExplain" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item label="加工(蒙)">
            <el-input v-model="form.mnProcess" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="加工(汉)">
            <el-input v-model="form.zhProcess" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="存储(蒙)">
            <el-input v-model="form.mnStore" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="存储(汉)">
            <el-input v-model="form.zhStore" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="参考资料(蒙)">
            <el-input v-model="form.mnReferenceMaterial" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item label="参考资料(汉)">
            <el-input v-model="form.zhReferenceMaterial" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item prop="status" label="状态">
            <template slot-scope="scope">
              <el-switch
                :active-value="1"
                :inactive-value="0"
                v-model="form.status"
                active-color="#13ce66"
                inactive-color="#ff4949">
              </el-switch>
            </template>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="id"/>
        <el-table-column prop="type" label="药品分类">
          <template slot-scope="scope">
            {{ dict.label.single_classification[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="mnName" label="名称(蒙)"/>
        <el-table-column prop="zhName" label="名称(汉)"/>
        <el-table-column prop="elName" label="名称(英)"/>
        <el-table-column prop="zhDocument" label="文献来源"/>
        <el-table-column prop="zhHabitat" label="生境"/>
        <el-table-column prop="zhMedicatedPart" label="入药部分"/>
        <el-table-column prop="zhNatureTaste" label="性味"/>
        <el-table-column prop="zhFeature" label="功能"/>
        <el-table-column prop="zhFunctionsDisease" label="主治"/>
        <el-table-column prop="zhSideEffect" label="副作用"/>
        <el-table-column prop="zhStore" label="存储"/>
        <el-table-column prop="createBy" label="创建者"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              changeEnabled
              @change="changeState(scope.row,scope.row.status)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','singleMedicine:edit','singleMedicine:del'])" label="操作" width="150px"
                         align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
            <el-button size="mini" type="success" icon="el-icon-upload2" @click="submit(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>

      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <el-upload
          multiple
          :limit="10"
          class="file-box"
          ref="upload"
          action="/upload"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-change="handleChange"
          :on-exceed="handleExceed"
          :file-list="images"
          list-type="picture"
          :auto-upload="false">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>

          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitFile()">上传到服务器
          </el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
        <el-row :gutter="20">
          <el-col v-for="url in imageurls" :span="6">
            <el-image
              style="width: 100px; height: 100px"
              :src="url"
              fit="fill"></el-image>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import crudSingleMedicine from '@/api/medicine/singleMedicine'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {mapGetters} from "vuex";
import {uploadImages} from "../../../api/medicine/singleMedicine";


const defaultForm = {
  id: null,
  type: null,
  mnName: null,
  mnNameCode: null,
  zhName: null,
  elName: null,
  mnAnotherName: null,
  mnAnotherNameCode: null,
  zhAnotherName: null,
  elAnotherName: null,
  mnScientificName: null,
  mnScientificNameCode: null,
  zhScientificName: null,
  elScientificName: null,
  mnDocument: null,
  mnDocumentCode: null,
  zhDocument: null,
  mnHabitat: null,
  zhHabitat: null,
  mnMedicatedPart: null,
  zhMedicatedPart: null,
  mnNatureTaste: null,
  mnNatureTasteCode: null,
  zhNatureTaste: null,
  mnFeature: null,
  mnFeatureCode: null,
  zhFeature: null,
  mnFunctionsDisease: null,
  mnFunctionsDiseaseCode: null,
  zhFunctionsDisease: null,
  mnSideEffect: null,
  mnSideEffectCode: null,
  zhSideEffect: null,
  mnClinicalUse: null,
  zhClinicalUse: null,
  mnExplain: null,
  zhExplain: null,
  mnProcess: null,
  mnProcessCode: null,
  zhProcess: null,
  mnStore: null,
  mnStoreCode: null,
  zhStore: null,
  mnReferenceMaterial: null,
  zhReferenceMaterial: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  status: null,
  remark: null
}
export default {
  name: 'SingleMedicine',
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['single_classification'],
  cruds() {
    return CRUD({
      title: '单药管理',
      url: 'api/singleMedicine',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: {...crudSingleMedicine}
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'singleMedicine:add'],
        edit: ['admin', 'singleMedicine:edit'],
        del: ['admin', 'singleMedicine:del']
      },
      headers: {
        Authorization: this.$store.getters.token,
      },
      images: [],
      imageurls: [],
      fileList: [],
      dialogVisible: false,
      selections: [],
      row: {},
      rules: {
        mnName: [
          {required: true, message: '请输入名称(蒙)', trigger: 'change'}
        ],
        zhName: [
          {required: true, message: '请输入名称(汉)', trigger: 'change'}
        ],
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    submit(row) {
      this.singleId = row.id;
      this.dialogVisible = true;

    },
    pictureManagement(row) {
      this.$router.push({path: '/medicine/singlePicture', query: {param: row.id}})
    },
    changeState(data, val) {
      this.$confirm('此操作将修改状态值为 ' + val + ' 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudSingleMedicine.edit(data).then(res => {
          this.$message({
            type: 'waring',
            message: '修改成功'
          });
        });
      }).catch(() => {
        if (val === 1) {
          data.status = 0;
        } else {
          data.status = 1;
        }
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    handleRemove(file, fileList) {
      this.images = fileList
    },
    //点击某个文件时的钩子
    handlePreview(file) {

    },
    //添加到上传列表时的钩子
    handleChange(file, fileList) {
      this.images = fileList

    },
    //文件超出个数限制时的钩子
    handleExceed() {
      this.$message.warning("文件超出10个")
    },
    getImages() {
      this.$axios.get("/filelist")
        .then(response => {
          this.imageurls = response.data

        })
        .catch(error => {
          this.$message.error("获取图片列表失败")
        })
    },
    submitFile() {
      if (this.images.length === 0) {
        this.$message.warning("请选择文件")
        return
      }
      const formData = new FormData();
      this.images.forEach(file => {
        formData.append("images", file.raw) //将文件传到表单中，files属性是后端接受的参数名
      })
      formData.append("singleId", this.singleId)
      uploadImages(formData).then(res => {
        this.$message({
          type: 'success',
          message: '图片上传成功!'
        })
        this.images = []
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '图片上传失败!'
        })
      })
    },

  },
  computed: {
    ...mapGetters([
      'fileUploadApi',
      'imagesUploadApi',
      'baseApi'
    ])
  }
}
</script>

<style scoped>

</style>
